package org.tarena.dang.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tarena.dang.dao.BookDAO;
import org.tarena.dang.dao.impl.BookDAOImpl;
import org.tarena.dang.entity.Book;

public class BookServiceImp implements BookService {
	BookDAO dao = new BookDAOImpl();

	@Override
	public List<Book> findByCid(int cid,int currentPage,int pageSize) throws SQLException {
		List<Book> books = dao.findByCid(cid,currentPage, pageSize);
		return books;
	}

	//暂停使用 详情参见findByTotalSize（）
	@Override
	public int findByCiddAll(int cid) throws SQLException {
		return dao.findByCiddAll(cid);
	}

	@Override
	public List<Book> findRecomBook(int n, int recomBookNnm) throws SQLException {
		List<Book> list = dao.findRecomBook(n,recomBookNnm);
		return list;
	}

	@Override
	public List<Book> findByPriceD(int cid, int currentPage, int pageSize)
			throws SQLException {
		return dao.findByPriceD(cid,currentPage,pageSize);
	}

	@Override
	public int getTotalPage(int totalSize, int pageSize) {
		return totalSize%pageSize==0?totalSize/pageSize:totalSize/pageSize+1;
	}

	@Override
	public List<Book> findBySort(int cid, int currentPage, int pageSize,
			String sort) throws SQLException{
		Map<String, String> map = new HashMap<String, String>();
		map.put("priceUp", "order by dang_price");
		map.put("timeUp", "order by add_time");
		map.put("hotUp", "order by product_num");
//		map.put(null, " ");
//		map.put("", "");
		String orderBy = map.get(sort);
		List<Book> list = dao.findBySort(cid,currentPage,pageSize,orderBy);
		return list;
	}

	@Override
	public List<Book> findByBook(int parentId, int cid, int currentPage,
			int pageSize, String sort) throws SQLException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("priceUp", "order by dang_price");
		map.put("timeUp", "order by add_time");
		map.put("hotUp", "order by product_num");
		String orderBy = map.get(sort);
		if(cid == 1){
			List<Book> list = dao.findBySortAll(parentId,currentPage,pageSize,orderBy);
			return list;
		}else{
			List<Book> list = dao.findBySort(cid,currentPage,pageSize,orderBy);
			return list;
		}
		
	}

	@Override
	public int findByTotalSize(int cid, int parentId) throws SQLException {
		if(cid == 1){
			return dao.findByPageSizeAll(parentId);
		}else{
			
			return dao.findByCiddAll(cid);
		}

	}

	@Override
	public List<Book> searchByKeyWords(String keyWords) throws SQLException {
		List<Book> list = dao.searchByKeyWords(keyWords);
		return list;
	}

	@Override
	public List<Book> searchByKeyWords(String keyWords, int cid)
			throws SQLException {
		if(cid == 1){
			List<Book> list = dao.searchByKeyWords(keyWords);
			return list;
		}else{
			List<Book> list = dao.searchByKeyWords(keyWords,cid);
			return list;
		}
		
	}

	@Override
	public Book findByProId(int id) throws SQLException {
		Book book = dao.findByProId(id);
		return book;
	}

	@Override
	public List<Book> findHotBooks() throws SQLException {
		List<Book> hotBooks = dao.findHotBooks();
		return hotBooks;
	}


}
